
# Find the mean for each cell across the 39 months

meanmaxmonth01 = rowMeans(maxmonthsummary01)
meanminmonth01 = rowMeans(minmonthsummary01)

meanmaxmonth02 = rowMeans(maxmonthsummary02)
meanminmonth02 = rowMeans(minmonthsummary02)

meanmaxmonth03 = rowMeans(maxmonthsummary03)
meanminmonth03 = rowMeans(minmonthsummary03)

meanmaxmonth04 = rowMeans(maxmonthsummary04)
meanminmonth04 = rowMeans(minmonthsummary04)

meanmaxmonth05 = rowMeans(maxmonthsummary05)
meanminmonth05 = rowMeans(minmonthsummary05)

meanmaxmonth06 = rowMeans(maxmonthsummary06)
meanminmonth06 = rowMeans(minmonthsummary06)

meanmaxmonth07 = rowMeans(maxmonthsummary07)
meanminmonth07 = rowMeans(minmonthsummary07)

meanmaxmonth08 = rowMeans(maxmonthsummary08)
meanminmonth08 = rowMeans(minmonthsummary08)

meanmaxmonth09 = rowMeans(maxmonthsummary09)
meanminmonth09 = rowMeans(minmonthsummary09)

meanmaxmonth10 = rowMeans(maxmonthsummary10)
meanminmonth10 = rowMeans(minmonthsummary10)

meanmaxmonth11 = rowMeans(maxmonthsummary11)
meanminmonth11 = rowMeans(minmonthsummary11)

meanmaxmonth12 = rowMeans(maxmonthsummary12)
meanminmonth12 = rowMeans(minmonthsummary12)

meanmaxmonth13 = rowMeans(maxmonthsummary13)
meanminmonth13 = rowMeans(minmonthsummary13)

meanmaxmonth14 = rowMeans(maxmonthsummary14)
meanminmonth14 = rowMeans(minmonthsummary14)

meanmaxmonth15 = rowMeans(maxmonthsummary14)
meanminmonth15 = rowMeans(minmonthsummary14)

meanmaxmonth16 = rowMeans(maxmonthsummary15)
meanminmonth16 = rowMeans(minmonthsummary15)

meanmaxmonth17 = rowMeans(maxmonthsummary16)
meanminmonth17 = rowMeans(minmonthsummary16)

meanmaxmonth18 = rowMeans(maxmonthsummary17)
meanminmonth18 = rowMeans(minmonthsummary17)

meanmaxmonth19 = rowMeans(maxmonthsummary18)
meanminmonth19 = rowMeans(minmonthsummary18)

meanmaxmonth20 = rowMeans(maxmonthsummary19)
meanminmonth20 = rowMeans(minmonthsummary19)

meanmaxmonth21 = rowMeans(maxmonthsummary20)
meanminmonth21 = rowMeans(minmonthsummary20)

meanmaxmonth22 = rowMeans(maxmonthsummary21)
meanminmonth22 = rowMeans(minmonthsummary21)

meanmaxmonth23 = rowMeans(maxmonthsummary23)
meanminmonth23 = rowMeans(minmonthsummary23)

meanmaxmonth24 = rowMeans(maxmonthsummary24)
meanminmonth24 = rowMeans(minmonthsummary24)

meanmaxmonth25 = rowMeans(maxmonthsummary25)
meanminmonth25 = rowMeans(minmonthsummary25)

meanmaxmonth26 = rowMeans(maxmonthsummary26)
meanminmonth26 = rowMeans(minmonthsummary26)

meanmaxmonth27 = rowMeans(maxmonthsummary27)
meanminmonth27 = rowMeans(minmonthsummary27)

meanmaxmonth28 = rowMeans(maxmonthsummary28)
meanminmonth28 = rowMeans(minmonthsummary28)

meanmaxmonth29 = rowMeans(maxmonthsummary29)
meanminmonth29 = rowMeans(minmonthsummary29)

meanmaxmonth30 = rowMeans(maxmonthsummary30)
meanminmonth30 = rowMeans(minmonthsummary30)

meanmaxmonth31 = rowMeans(maxmonthsummary31)
meanminmonth31 = rowMeans(minmonthsummary31)

meanmaxmonth32 = rowMeans(maxmonthsummary32)
meanminmonth32 = rowMeans(minmonthsummary32)

meanmaxmonth33 = rowMeans(maxmonthsummary33)
meanminmonth33 = rowMeans(minmonthsummary33)

meanmaxmonth34 = rowMeans(maxmonthsummary34)
meanminmonth34 = rowMeans(minmonthsummary34)

meanmaxmonth35 = rowMeans(maxmonthsummary35)
meanminmonth35 = rowMeans(minmonthsummary35)

meanmaxmonth36 = rowMeans(maxmonthsummary36)
meanminmonth36 = rowMeans(minmonthsummary36)

meanmaxmonth37 = rowMeans(maxmonthsummary37)
meanminmonth37 = rowMeans(minmonthsummary37)

meanmaxmonth38 = rowMeans(maxmonthsummary38)
meanminmonth38 = rowMeans(minmonthsummary38)

meanmaxmonth39 = rowMeans(maxmonthsummary39)
meanminmonth39 = rowMeans(minmonthsummary39)

meanmaxmonth40 = rowMeans(maxmonthsummary40)
meanminmonth40 = rowMeans(minmonthsummary40)

meanmaxmonth41 = rowMeans(maxmonthsummary41)
meanminmonth41 = rowMeans(minmonthsummary41)

meanmaxmonth42 = rowMeans(maxmonthsummary42)
meanminmonth42 = rowMeans(minmonthsummary42)

meanmaxmonth43 = rowMeans(maxmonthsummary43)
meanminmonth43 = rowMeans(minmonthsummary43)

meanmaxmonth44 = rowMeans(maxmonthsummary44)
meanminmonth44 = rowMeans(minmonthsummary44)

meanmaxmonth45 = rowMeans(maxmonthsummary45)
meanminmonth45 = rowMeans(minmonthsummary45)

meanmaxmonth46 = rowMeans(maxmonthsummary46)
meanminmonth46 = rowMeans(minmonthsummary46)

meanmaxmonth47 = rowMeans(maxmonthsummary47)
meanminmonth47 = rowMeans(minmonthsummary47)

meanmaxmonth48 = rowMeans(maxmonthsummary48)
meanminmonth48 = rowMeans(minmonthsummary48)

meanmaxmonth49 = rowMeans(maxmonthsummary49)
meanminmonth49 = rowMeans(minmonthsummary49)

meanmaxmonth50 = rowMeans(maxmonthsummary50)
meanminmonth50 = rowMeans(minmonthsummary50)

meanmaxmonth51 = rowMeans(maxmonthsummary51)
meanminmonth51 = rowMeans(minmonthsummary51)

meanmaxmonth52 = rowMeans(maxmonthsummary52)
meanminmonth52 = rowMeans(minmonthsummary52)

### Remove month summaries now

rm(list=ls()[grep('summary',ls())])

# Bind monthly summaries together

maxtobc = cbind(meanmaxmonth01,meanmaxmonth02,meanmaxmonth03,meanmaxmonth04,meanmaxmonth05,meanmaxmonth06,meanmaxmonth07,meanmaxmonth08,meanmaxmonth09,meanmaxmonth10,meanmaxmonth11,meanmaxmonth12,meanmaxmonth13,meanmaxmonth14,meanmaxmonth15,meanmaxmonth16,meanmaxmonth17,meanmaxmonth18,meanmaxmonth19,meanmaxmonth20,meanmaxmonth21,meanmaxmonth22,meanmaxmonth23,meanmaxmonth23,meanmaxmonth24,meanmaxmonth25,meanmaxmonth26,meanmaxmonth27,meanmaxmonth28,meanmaxmonth29,meanmaxmonth30,meanmaxmonth31,meanmaxmonth32,meanmaxmonth33,meanmaxmonth34,meanmaxmonth35,meanmaxmonth36,meanmaxmonth37,meanmaxmonth38,meanmaxmonth39,meanmaxmonth40,meanmaxmonth41,meanmaxmonth42,meanmaxmonth43,meanmaxmonth44,meanmaxmonth45,meanmaxmonth46,meanmaxmonth47,meanmaxmonth48,meanmaxmonth49,meanmaxmonth50,meanmaxmonth51,meanmaxmonth52)
mintobc = cbind(meanminmonth01,meanminmonth02,meanminmonth03,meanminmonth04,meanminmonth05,meanminmonth06,meanminmonth07,meanminmonth08,meanminmonth09,meanminmonth10,meanminmonth11,meanminmonth12,meanminmonth13,meanminmonth14,meanminmonth15,meanminmonth16,meanminmonth17,meanminmonth18,meanminmonth19,meanminmonth20,meanminmonth21,meanminmonth22,meanminmonth23,meanminmonth23,meanminmonth24,meanminmonth25,meanminmonth26,meanminmonth27,meanminmonth28,meanminmonth29,meanminmonth30,meanminmonth31,meanminmonth32,meanminmonth33,meanminmonth34,meanminmonth35,meanminmonth36,meanminmonth37,meanminmonth38,meanminmonth39,meanminmonth40,meanminmonth41,meanminmonth42,meanminmonth43,meanminmonth44,meanminmonth45,meanminmonth46,meanminmonth47,meanminmonth48,meanminmonth49,meanminmonth50,meanminmonth51,meanminmonth52)

### Remove meanmonthly summaries now

rm(list=ls()[grep('meanmaxmonth',ls())])
rm(list=ls()[grep('meanminmonth',ls())])

# Run BC function

bcs = bioclim(tmin=mintobc,tmax=maxtobc,vois=c(1:6), cov=TRUE) # Modified this call

### Remove maxtobc and mintobc

rm(list=ls()[grep('tobc',ls())])

# Read in ASCII file

base.asc = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/STATIC/dem_WTplusbuffer_LatLong_WGS1984_250mres.asc')

# Write out microCLIM files

for (i in c(1,4,5,6))

	{
	
	base.asc[which(is.na(base.asc)==F)]=bcs[,i]
	
	write.asc.gz(base.asc, file=paste(out.dir,'microCLIM_0',i,sep=''))
	
	}
	
# Close loop
